import requests
from bs4 import BeautifulSoup
def gethtml(url):
    try:
        re=requests.get(url,timeout=10)
        re.raise_for_status()
        re.encoding="utf-8"
        print("获取成功")
        return re.text
    except:
        print("获取失败")
        return ""
def gethtmldata(html):
    soup=BeautifulSoup(html,"html.parser")
    div_ls=soup.find_all("div",{"class":"rili sudynews_rili_2"})
    all_ls=[]
    li_ls=div_ls[0].find_all("li")
    for li in li_ls:
        ls=[]
        div_year=li.find_all("div",{"class":"news_year"})
        div_days=li.find_all("div",{"class":"news_days"})
        div_titles=li.find_all("div",{"class":"news_title"})
        a1 = div_titles[0].find("a")
        ls.append(div_year[0].string +"."+ div_days[0].string)
        ls.append(a1.attrs["href"])
        all_ls.append(ls)
    return all_ls
def writedata(ls):
    with open(r"威海职业技术学院.csv","w") as f:
        for line in ls:
            f.write(",".join(line)+"\n")
def main():
    url="http://www.weihaicollege.com/main.htm"
    html=gethtml(url)
    li=gethtmldata(html)
    writedata(li)
if __name__ == '__main__':
    main()